/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.ejweixin.domain.NgConstructionProgress;
import me.zhengjie.modules.ejweixin.service.NgConstructionProgressService;
import me.zhengjie.modules.ejweixin.domain.vo.NgConstructionProgressQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.modules.ejweixin.service.NgConstructionSiteInfoService;
import me.zhengjie.utils.Result;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author cy
* @date 2025-04-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "工地进度管理")
@RequestMapping("/api/ngConstructionProgress")
public class NgConstructionProgressController {

    private final NgConstructionProgressService ngConstructionProgressService;

    private final NgConstructionSiteInfoService ngConstructionSiteInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ngConstructionProgress:list')")
    public void exportNgConstructionProgress(HttpServletResponse response, NgConstructionProgressQueryCriteria criteria) throws IOException {
        ngConstructionProgressService.download(ngConstructionProgressService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询工地进度")
    @ApiOperation("查询工地进度")
    @PreAuthorize("@el.check('ngConstructionProgress:list')")
    public ResponseEntity<PageResult<NgConstructionProgress>> queryNgConstructionProgress(NgConstructionProgressQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(ngConstructionProgressService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("progressList")
    @Log("查询工地进度")
    @ApiOperation("查询工地进度")
    @AnonymousAccess
    public ResponseEntity<PageResult<NgConstructionProgress>> progressList(NgConstructionProgressQueryCriteria criteria, Page<Object> page){
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            criteria.setUserId(userId.intValue());
        }catch (Exception e){
            System.out.println("未找到登陆的用户信息");
        }
        return new ResponseEntity<>(ngConstructionProgressService.progressList(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增工地进度")
    @ApiOperation("新增工地进度")
    @AnonymousAccess
    public ResponseEntity<Object> createNgConstructionProgress(@Validated @RequestBody NgConstructionProgress resources){
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            String userName = SecurityUtils.getCurrentUsername();
            resources.setStaffId(userId.intValue());
            resources.setStaffName(userName);
            ngConstructionSiteInfoService.updateStaffViewCount(resources.getSiteId(), userId);
        }catch (BadRequestException e){
            System.out.println("未找到登陆的用户信息");
        }
        ngConstructionProgressService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改工地进度")
    @ApiOperation("修改工地进度")
    @PreAuthorize("@el.check('ngConstructionProgress:edit')")
    public ResponseEntity<Object> updateNgConstructionProgress(@Validated @RequestBody NgConstructionProgress resources){
        ngConstructionProgressService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除工地进度")
    @ApiOperation("删除工地进度")
    @PreAuthorize("@el.check('ngConstructionProgress:del')")
    public ResponseEntity<Object> deleteNgConstructionProgress(@RequestBody List<Integer> ids) {
        ngConstructionProgressService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping("deleteProgress")
    @Log("删除工地进度")
    @ApiOperation("删除工地进度")
    public Result<Object> deleteProgress(Integer id) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            NgConstructionProgress constructionProgress = ngConstructionProgressService.getById(id);
            if(null != constructionProgress){
                Integer staffId = constructionProgress.getStaffId();
                if(userId.equals(staffId.longValue())){
                    List ids = new ArrayList<Integer>();
                    ids.add(id);
                    ngConstructionProgressService.deleteAll(ids);
                    return Result.success("删除成功");
                }else{
                    return Result.fail("500", "删除失败，没有权限删除！");
                }
            }else{
                return Result.fail("500", "未找到进度信息！");
            }
        }catch (BadRequestException e){
            System.out.println("未找到登陆的用户信息");
            return Result.fail("500", "未找到登陆的用户信息！");
        }
    }
}